package com.gxs.ea.fileporta.data.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.QueryBuilder;

@Service
public class FileMetaCollectionServiceImpl implements FileMetaCollectionService
{
	@Autowired
	private MongoTemplate mongoTemplate;

	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

	@Override
	public BasicDBObject insertDBObject(String collectionName,
			BasicDBObject object) 
	{
		MongoOperations operations = (MongoOperations) mongoTemplate;
		operations.save(object, collectionName);
		return object;
	}

	@Override
	public List<DBObject> listAll(String collectionName, int startIndex, int pageSize, String sortColumn, int sortOrder, Map<String, String> filters, Map<String, String> mapOfSearchFields) 
	{
		QueryBuilder queryObject = new QueryBuilder();
		QueryBuilder queryObject1 = new QueryBuilder();
		
		for(String key :  filters.keySet() )
		{
			String filterValue = filters.get(key);
			String regularExpressionString = "^===";
			regularExpressionString = regularExpressionString.replace("===", filterValue);
			queryObject.put(key).regex(Pattern.compile(regularExpressionString, Pattern.CASE_INSENSITIVE));
		}
		
		for(String fieldName : mapOfSearchFields.keySet() )
		{
			String filterValue = mapOfSearchFields.get(fieldName);
			if(org.apache.commons.lang.StringUtils.isEmpty(filterValue))
				continue;
			String regularExpressionString = "^===";
			regularExpressionString = regularExpressionString.replace("===", filterValue);
			
			
			queryObject1.and(fieldName).regex(Pattern.compile(regularExpressionString, Pattern.CASE_INSENSITIVE));
		}
		
		queryObject.and(queryObject1.get());
		
		MongoOperations operation = (MongoOperations) mongoTemplate;
		
		DBCursor cur = operation.getCollection(collectionName).find(queryObject.get()).sort(new BasicDBObject(sortColumn, sortOrder)).skip(startIndex).limit(pageSize);
		List<DBObject> list = new ArrayList<DBObject>();
		while(cur.hasNext())
		{
			list.add(   cur.next() );
		}

		return list;
	}

	@Override
	public long countAll(String collectionName, int startIndex, int pageSize, Map<String, String> filters, Map<String, String> mapOfSearchFields) 
	{
		
		QueryBuilder queryObject = new QueryBuilder();
		QueryBuilder queryObject1 = new QueryBuilder();
		
		
		for(String key :  filters.keySet() )
		{
			String filterValue = String.valueOf( filters.get(key) );
			String regularExpressionString = "^===";
			regularExpressionString = regularExpressionString.replace("===", filterValue);
			queryObject.put(key).regex(Pattern.compile(regularExpressionString, Pattern.CASE_INSENSITIVE));
		}
		
		for(String fieldName : mapOfSearchFields.keySet() )
		{
			String filterValue = mapOfSearchFields.get(fieldName);
			if(org.apache.commons.lang.StringUtils.isEmpty(filterValue))
				continue;
			String regularExpressionString = "^===";
			regularExpressionString = regularExpressionString.replace("===", filterValue);
			queryObject1.and(fieldName).regex(Pattern.compile(regularExpressionString, Pattern.CASE_INSENSITIVE));
		}

		queryObject.and(queryObject1.get());
		
		MongoOperations operation = (MongoOperations) mongoTemplate;
		long count = operation.getCollection(collectionName).find(queryObject.get()).count();
		
		return count;
	}
	
	
}
